package com.kymatrix.kycarbon.xsmd.system.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.InsertUserReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.ListQueryUserResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.PageQueryUserReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.PageQueryUserResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.QueryUserAuthResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.QueryUserResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.UpdateUserPwdReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.user.UpdateUserReq;
import com.kymatrix.kycarbon.xsmd.system.server.service.IUserService;
import com.kymatrix.kycarbon.xsmd.common.business.common.PageResp;
import com.kymatrix.kycarbon.xsmd.common.business.common.ResultResp;
import com.kymatrix.kycarbon.xsmd.common.springsecurity.LoginUser;
import com.kymatrix.kycarbon.xsmd.common.springsecurity.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(tags = "用户管理")
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 添加用户
     */
    @ApiOperation("添加用户")
    @PostMapping("/user/add")
    public ResultResp<String> addUser(@RequestBody @Validated InsertUserReq user) {
        synchronized (UserController.class) {
            userService.save(user);
            return ResultResp.ok();
        }
    }

    /**
     * 编辑用户
     */
    @ApiOperation("编辑用户")
    @PostMapping("/user/update")
    public ResultResp<String> updateUser(@RequestBody @Validated UpdateUserReq req) {
        synchronized (UserController.class) {
            userService.update(req);
            return ResultResp.ok();
        }
    }

    @ApiOperation("删除用户")
    @GetMapping("/user/delete")
    public ResultResp<String> deleteUser(@RequestParam("id") String id) {
        userService.deleteUser(id);
        return ResultResp.ok();
    }

    @ApiOperation("分页获取用户列表")
    @PostMapping("/user/pageQuery")
    public ResultResp<PageResp<PageQueryUserResp>> pageQuery(@RequestBody @Validated PageQueryUserReq req) {
        Page<PageQueryUserResp> res = userService.pageQuery(req);
        return ResultResp.ok(PageResp.build(res));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/user/list")
    public ResultResp<List<ListQueryUserResp>> list(@RequestParam(value = "username", required = false)
                                                    @ApiParam(value = "username") String username) {
        List<ListQueryUserResp> res = userService.list(username);
        return ResultResp.ok(res);
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/user/detail")
    public ResultResp<QueryUserResp> getUser(@RequestParam("id") String id) {
        QueryUserResp res = userService.getUser(id);
        return ResultResp.ok(res);
    }

    /**
     * 查询登录用户权限
     */
    @ApiOperation("查询登录用户权限")
    @GetMapping("/user/auth")
    public ResultResp<QueryUserAuthResp> auth() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        QueryUserAuthResp res = userService.auth(loginUser.getId());
        return ResultResp.ok(res);
    }

    @ApiOperation("修改密码")
    @PostMapping("/user/editPwd")
    public ResultResp<String> editPwd(@RequestBody UpdateUserPwdReq req) {
        userService.editPassword(req);
        return ResultResp.ok();
    }
}
